package com.kzw.misc.freemarker.tags;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.kzw.misc.utils.ContextUtil;
import com.kzw.oa.system.model.AppUser;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 授权标签 <@grant key="role_add">，
 */
@Component
public class GrantDirective implements TemplateDirectiveModel {

	/**
	 * 参数key:
	 */
	@Override
	@SuppressWarnings("rawtypes")
	public void execute(Environment env, Map map, TemplateModel[] loopVars, TemplateDirectiveBody body)
			throws TemplateException, IOException {

		// 能否获得当前用户？
		AppUser currUser = ContextUtil.getCurrentUser();
		if (currUser != null) {
			Set<String> rights = currUser.getRights();
			if (rights.contains("__ALL")) {
				body.render(env.getOut());
			}

			if (map.containsKey("key") && map.get("key") != null) {
				String key = map.get("key").toString();
				if (rights.contains(key)) {
					body.render(env.getOut());
				}
			}
		}

	}

}
